<?php
/**
 * Display a single prodcut
 *
 * @access public
 * @param array $atts
 * @return string
 */
function woocommerce_product_variation($atts) {
  	
  	
	if (empty($atts)) return;

  	global $wpdb, $woocommerce;

  	if (!isset($atts['style'])) $atts['style'] = 'border:4px solid #ccc; padding: 12px;';

  	if ($atts['id']) :
  		$product_data = get_post( $atts['id'] );
	elseif ($atts['sku']) :
		$product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $atts['sku']));
		$product_data = get_post( $product_id );
	else :
		return;
	endif;
	

	
	if ($product_data->post_type=='product') {

		$product = $woocommerce->setup_product_data( $product_data );
		$available_variations = $product->get_available_variations( );
		
		$default_attr = $product->get_variation_default_attributes( );
		//print_r("<pre>");
		//print_r($available_variations);
		//print_r("</pre>");
		ob_start();
		
		?>
		<script type="text/javascript">
			var product_variations_<?php echo $product->id; ?> = <?php echo json_encode( $available_variations ) ?>;
		</script>
		<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo $product->id; ?>" id="form_<?php echo $product->id; ?>" name="form_<?php echo $product->id; ?>">
		
		<blockquote>
			<h3>
				<button type="submit" class="single_add_to_cart_button button alt">&nbsp;</button>
				<?php echo $product->get_image() ?>
				<?php 
				$titles = explode('|', $product->get_title( )); 
				echo $titles[1];
					
				?>
			</h3>
			<p style="padding-left : 80px;">
			<?php

			foreach ( $available_variations as $variation ) {
				if($default_attr['size'] == $variation['attributes']['attribute_size']) {
					echo "<input type='radio' name='variation_id' id='variation_id' value='" . $variation['variation_id'] . "' checked /> " . $variation['attributes']['attribute_size'] . ' ' . $variation['price_html'] . '<br />';
				} else {
					echo "<input type='radio' name='variation_id' id='variation_id' value='" . $variation['variation_id'] . "' /> " . $variation['attributes']['attribute_size'] . ' ' . $variation['price_html'] .   '<br />';
				}
			}
			?>
			</p>
		</blockquote>
		
		<input type="hidden" id="quantity" name="quantity" value="1" />
		<input type="hidden" id="attribute_size" name="attribute_size" value="30 x 40 cm" />
		<input type="hidden" id="product_id" name="product_id" value="<?php echo esc_attr( $product->id ); ?>" />
		
		</form>

		<?php

		//print_r("<pre>");
		//print_r($product);
		//print_r($product->get_available_variations( ));
		//print_r("</pre>");
		
		return ob_get_clean();

	} elseif ($product_data->post_type=='product_variation') {

		return ob_get_clean();
	}
}

function woocommerce_product_variation_url($atts, $content = null) {
	if (empty($atts)) return;

  	global $wpdb, $woocommerce;

  	if (!isset($atts['style'])) $atts['style'] = 'border:4px solid #ccc; padding: 12px;';

  	if ($atts['id']) :
  		$product_data = get_post( $atts['id'] );
	elseif ($atts['sku']) :
		$product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $atts['sku']));
		$product_data = get_post( $product_id );
	else :
		return;
	endif;
	

	
	if ($product_data->post_type=='product') {

		$product = $woocommerce->setup_product_data( $product_data );
		$available_variations = $product->get_available_variations( );
		
		ob_start();
		
		
		echo '<a href="" id="add_to_cart_' . $product->id . '" data-product_id="' . $product->id . '">' . $content . '</a>';
		echo '<ul id="list-size-' . $product->id . '" style="display : none">';
		foreach ( $available_variations as $variation ) {
			echo '<li data-product_id ="' . $product->id . '" data-variation_id="'.$variation['variation_id'].'" data-variation_size="'. $variation['attributes']['attribute_size'] . '">'. $variation['attributes']['attribute_size'] . '</li>';
		}
		echo '</ul>';
		return ob_get_clean();

	} elseif ($product_data->post_type=='product_variation') {

		return ob_get_clean();
	}
}
add_shortcode('product_variation', 'woocommerce_product_variation');
add_shortcode('product_variation_url', 'woocommerce_product_variation_url');
?>